package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.qo.QueryObject;
import cn.wolfcode.rbac.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role) {
        roleMapper.save(role);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        roleMapper.delete(id);
    }

    @Override
    @Transactional
    public void deleteRelation(Long id){
        roleMapper.deleteRelation(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        roleMapper.update(role);
    }

    @Override
    @Transactional
    public Role get(Long id) {
        return roleMapper.get(id);
    }

    @Override
    @Transactional
    public List<Role> listAll() {
        return roleMapper.listAll();
    }

    /*@Override
    @Transactional
    public PageResult<Role> query(QueryObject qo) {
        int count = roleMapper.selectForCount(qo);
        if(count==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(), Collections.emptyList(),count);
        }

        List<Role> PageResult = roleMapper.selectForList(qo);

        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),PageResult,count);
    }*/

    public PageInfo<Role> query(QueryObject qo){
        // 使用分插件，当传入前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo(roles);
    }

    @Override
    public void insertRelation(Long id, Long permissionId) {
        roleMapper.insertRelation(id,permissionId);
    }




}
